<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
  header("Location:checkLogin.php");
}
else
{
	$status = isset($_REQUEST['status']) && $_REQUEST['status'] != '' ? $_REQUEST['status'] : "";
	if(isset($_POST['submit']))
	{
	  $partyId      = isset($_REQUEST['partyId']) && $_REQUEST['partyId'] != '' ? $_REQUEST['partyId'] : 0;
	  $purchaseDate = $_POST['purchaseDateYear']."-".$_POST['purchaseDateMonth']."-".$_POST['purchaseDateDay'];
	  
	  $insertMaster = "INSERT INTO purchasemaster (partyId,purchaseDate,status)
	                                       VALUES (".$partyId.",'".$purchaseDate."','".$status."')";
	  $insertMasterRes = mysql_query($insertMaster);
	  $lastMasterId = mysql_insert_id();
	  if(!$insertMasterRes)
	  {
	  	echo "Insert Fail";
	  }
	  else
	  {
	  	$loopCount = 0;
      while($loopCount < count($_POST['itemId']))
      {
        $itemId   = isset($_POST['itemId'][$loopCount]) && ($_POST['itemId'][$loopCount] != '') ? $_POST['itemId'][$loopCount] : 0;
        $quantity = isset($_POST['quantity'][$loopCount]) && ($_POST['quantity'][$loopCount] != '') ? $_POST['quantity'][$loopCount] : 0;
        $fat      = isset($_POST['fat'][$loopCount]) && ($_POST['fat'][$loopCount] != '') ? $_POST['fat'][$loopCount] : 0;
        $fatRate  = isset($_POST['fatRate'][$loopCount]) && ($_POST['fatRate'][$loopCount] != '') ? $_POST['fatRate'][$loopCount] : 0;
        $rate     = isset($_POST['rate'][$loopCount]) && ($_POST['rate'][$loopCount] != '') ? $_POST['rate'][$loopCount] : 0;
        $amount   = isset($_POST['amount'][$loopCount]) && ($_POST['amount'][$loopCount] != '') ? $_POST['amount'][$loopCount] : 0;
        if($status == 'P')
        {
          $plusMinus = '+';
        }
        else
        {
        	$plusMinus = '-';
        }
        if($_POST['amount'][$loopCount] != "")
        {
          $detailQuery = "INSERT INTO purchasedetail (purchaseMasterId,itemId,quantity,fat,fatRate,rate,amount)
                                              VALUES (".$lastMasterId.",".$itemId.",".$quantity.",".$fat.",".$fatRate.",".$rate.",".$amount.")";
          $detailQueryRes = mysql_query($detailQuery);
          insertItemStock($itemId,$quantity,$plusMinus);
        }
        $loopCount++;
      }
	    header("Location:purchase.php?status=".$status."");
	  }
	}
	$itemArray = array();
	$i = 0;
	$selectItem = "SELECT itemId,itemName,itemUnit
	                 FROM item
	                ORDER BY itemName";
	$selectItemRes = mysql_query($selectItem);
	while($itemRow = mysql_fetch_array($selectItemRes))
	{
		$itemArray['itemId'][$i]   = $itemRow['itemId'];
		$itemArray['itemName'][$i] = $itemRow['itemName'];
		$i++;
	}
	$partyArray = array();
	$i = 0;
	$selectParty = "SELECT partyId,partyName
	                  FROM party
	                ORDER BY partyName";
	$selectPartyRes = mysql_query($selectParty);
	while($partyRow = mysql_fetch_array($selectPartyRes))
	{
		$partyArray['partyId'][$i]   = $partyRow['partyId'];
		$partyArray['partyName'][$i] = $partyRow['partyName'];
		$i++;
	}
}

include("./bottom.php");
$smarty->assign('itemArray',$itemArray);
$smarty->assign('partyArray',$partyArray);
$smarty->assign('status',$status);
$smarty->display("purchase.tpl");
?>